<?php
namespace App\Service\Pay;

use App\Models\Log\LogOnlinepay;

use Carbon\Carbon;

abstract class OnlinePayService
{

    private $params = [];

    public function __construct($params)
    {
        $this->params = $params;
    }

    protected function set($params) {
        $this->params = $params;
    }

    protected function get($key, $default=null)
    {
        if (isset($this->params[$key])) {
            return $this->params[$key];
        }
        return $default;
    }

    protected function getAll()
    {
        return $this->params;
    }

    /**
     * 保存在线支付请求日志
     * @param int $order_no 订单号
     * @param int $order_type 订单类型
     * @param int $platform 支付平台
     * @param int $pay_type_id 支付方式id
     * @param array $request 请求数据
     * @return bool
     */
    protected function addLog($order_no, $order_type, $platform, $pay_type_id, $request)
    {

        $online_pay_log = LogOnlinepay::find($order_no);
        if (!$online_pay_log) {
            $online_pay_log = new LogOnlinepay();
            $online_pay_log->uuid = makeUuid();
            $online_pay_log->timeStamp = Carbon::now();
            $online_pay_log->creator = 'system_onlinepay';
            $online_pay_log->createTime = Carbon::now();
            $online_pay_log->bill_no = $order_no;
        }
        $online_pay_log->payTypeID = $pay_type_id;
        $online_pay_log->payTypeName = '微信支付';
        $online_pay_log->request_str = print_r($request, true);
        $online_pay_log->beginTime = date('Y-m-d H:i:s');
        $online_pay_log->request_flg = 1;
        $online_pay_log->success_count = 0;
        $online_pay_log->fail_count = 0;
        $online_pay_log->pay_amount = $request['total_fee'] / 100;
        $online_pay_log->billType = $order_type;
        $online_pay_log->save();

        return true;

    }

    /**
     * 更新在线支付请求日志
     * @param int $order_no 订单号
     * @param int $pay_status 支付状态 2、失败 3、成功
     * @param string|null $error_msg 错误信息
     * @return bool
     */
    protected function updateLog($order_no, $pay_status, $error_msg = null)
    {
        $online_pay_log = LogOnlinepay::find($order_no);
        if (!$online_pay_log) {
            return false;
        }
        $online_pay_log->server_response_str = print_r($this->params, true);
        $online_pay_log->request_flg = $pay_status;
        if ($pay_status == 3) {
            $online_pay_log->success_count += 1;
        } else {
            $online_pay_log->success_count += 0;
        }
        $online_pay_log->save();

        return true;

    }

    /**
     * 生成签名
     * @param array $params 签名参数
     * @param string $md5_key md5密钥
     * @return string
     */
    protected function createMD5Sign($params, $md5_key) {

        ksort($params);

        $sign_str = '';
        foreach($params as $k => &$v) {
            if ($v === '' || $v === null || in_array($k, ['sign', '_url'])) {
                continue;
            }
            if ($sign_str == '') {
                $sign_str .= $k . '=' . $v;
            } else {
                $sign_str .= '&' . $k . '=' . $v;
            }
        }
        $sign_str .= '&key=' . $md5_key;

        return strtoupper(md5($sign_str));

    }

    /**
     * 支付请求
     * @return mixed
     */
    abstract public function request();

    /**
     * 支付异步通知
     * @return mixed
     */
    abstract public function notify();

}